﻿using Abp.Extensions;
using Abp.Linq.Extensions;
using Mt.Site.Application.AccessStatistics.Dto;
using Mt.Site.Core.AccessStatistics.Entities;
using System.Linq;

namespace Mt.Site.Application.AccessStatistics
{
    public static class VisitExtension
    {
        /// <summary>
        /// 趋势统计条件
        /// </summary>
        /// <param name="query"></param>
        /// <param name="input"></param>
        /// <param name="TenancyName"></param>
        /// <returns></returns>
        public static IQueryable<AccessData> TreandStatistics(this IQueryable<AccessData> query, PagedTrendStatisticDataInput input,string TenancyName)
        {
            var endTime = input.EndTime.AddDays(1);
            query = query.WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                        item => item.ClientIp == input.Filter || item.Acount == input.Filter || item.Browser.Contains(input.Filter) || item.Searchkey.Contains(input.Filter))
                        .WhereIf(input.Source != null, item => item.SourceType == input.Source)
                        .WhereIf(input.Device != null, item => item.FormFactor == input.Device)
                        .WhereIf(!input.Search.IsNullOrWhiteSpace(), item => item.EngineType == input.Search)
                        .Where(item => item.VisitTime >= input.StartTime && item.VisitTime < endTime)
                        .Where(item=>item.TenancyName == TenancyName);

            return query;
        }

        #region 来源分析
        public static IQueryable<AccessData> AllSourceAnalysisQuery(this IQueryable<AccessData> query, SourceAnalysisInput input, string TenancyName)
        {
            var endTime = input.EndTime.AddDays(1);
            query = query.WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                          item => item.City.Contains(input.Filter) || item.Browser.Contains(input.Filter) || item.Title.Contains(input.Filter))
                          .WhereIf(input.Device != null, item => item.FormFactor == input.Device)
                          .Where(item => item.VisitTime >= input.StartTime && item.VisitTime < endTime)
                          .Where(item=>item.TenancyName == TenancyName);
            return query;
        }

        public static IQueryable<AccessData> SearchEngineAnalysisQuery(this IQueryable<AccessData> query, SourceSearchEngineInput input,string TenancyName)
        {
            var endTime = input.EndTime.AddDays(1);
            query = query.WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                            item => item.City.Contains(input.Filter) || item.Browser.Contains(input.Filter)
                            || item.Title.Contains(input.Filter) || item.EngineName.Contains(input.Filter))
                            .WhereIf(input.Device != null, item => item.FormFactor == input.Device)
                            .Where(item => item.VisitTime >= input.StartTime && item.VisitTime < endTime)
                            .Where(item => item.SourceType == SourceType.SearchEngines)
                            .Where(item=>item.TenancyName == TenancyName);
            return query;
        }

        public static IQueryable<AccessData> SearchKeyAnalysisQuery(this IQueryable<AccessData> query, SourceSearchKeyInput input, string TenancyName)
        {
            var endTime = input.EndTime.AddDays(1);
            query = query.WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                            item => item.City.Contains(input.Filter) || item.Browser.Contains(input.Filter)
                            || item.Title.Contains(input.Filter) || item.EngineName.Contains(input.Filter))
                            .Where(item => item.VisitTime >= input.StartTime && item.VisitTime < endTime)
                            .Where(item => item.SourceType == SourceType.SearchEngines)
                            .Where(item=>item.TenancyName == TenancyName);
            return query;
        }

        public static IQueryable<AccessData> ExternalLinkAnalysisQuery(this IQueryable<AccessData> query, SourceExternalLinkInput input, string TenancyName)
        {
            var endTime = input.EndTime.AddDays(1);
            query = query.WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                            item => item.City.Contains(input.Filter) || item.Browser.Contains(input.Filter)
                            || item.Title.Contains(input.Filter) || item.Searchkey.Contains(input.Filter))
                            .Where(item => item.VisitTime >= input.StartTime && item.VisitTime < endTime)
                            .Where(item => item.SourceType == SourceType.ExternalLinks)
                            .Where(item=>item.TenancyName == TenancyName);
            return query;
        }
        #endregion

        #region 页面分析
        public static IQueryable<AccessData> PagesAnalysisQuery(this IQueryable<AccessData> query, PagesAnalysisInput input,string TenancyName)
        {
            var endTime = input.EndTime.AddDays(1);
            query = query.WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                            item => item.City.Contains(input.Filter) || item.Url.Contains(input.Filter)
                            || item.Title.Contains(input.Filter) || item.Searchkey.Contains(input.Filter))
                            .WhereIf(input.Device != null, item => item.FormFactor == input.Device)
                            .Where(item => item.VisitTime >= input.StartTime && item.VisitTime < endTime)
                            .Where(item=>item.TenancyName == TenancyName);
            return query;
        }

        public static IQueryable<AccessData> PagesMenuAnalysisQuery(this IQueryable<AccessData> query, PageMenuAnalysisInput input, string TenancyName)
        {
            var endTime = input.EndTime.AddDays(1);
            query = query.WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                            item => item.City.Contains(input.Filter) || item.Url.Contains(input.Filter)
                            || item.Title.Contains(input.Filter) || item.Searchkey.Contains(input.Filter))
                            .Where(item => item.MenuId != null)
                            .WhereIf(input.Device != null, item => item.FormFactor == input.Device)
                            .Where(item => item.VisitTime >= input.StartTime && item.VisitTime < endTime)
                            .Where(item=>item.TenancyName == TenancyName);
            return query;

        }
        #endregion

        #region 访客分析
        public static IQueryable<AccessData> VisitorAreaAnalysisQuery(this IQueryable<AccessData> query, VisitorRegionInput input, string TenancyName)
        {
            var endTime = input.EndTime.AddDays(1);
            query = query.WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                            item => item.City.Contains(input.Filter) || item.Url.Contains(input.Filter)
                            || item.Title.Contains(input.Filter) || item.Searchkey.Contains(input.Filter))
                            .Where(item => item.VisitTime >= input.StartTime && item.VisitTime < endTime)
                            .Where(item=>item.TenancyName == TenancyName);
            return query;

        }

        public static IQueryable<AccessData> VisitorCityOrIspDetailQuery(this IQueryable<AccessData> query, VisitorAreaOrIpsInut input, string TenancyName)
        {
            var endTime = input.EndTime.AddDays(1);
            query = query.Where(item => item.Region == input.Area)
                            .Where(item => item.VisitTime >= input.StartTime && item.VisitTime < endTime)
                            .Where(item=>item.TenancyName == TenancyName);
            return query;

        }

        public static IQueryable<AccessData> VisitorSystemEnvQuery(this IQueryable<AccessData> query, SystemEnvInput input, string TenancyName)
        {
            var endTime = input.EndTime.AddDays(1);
            query = query.WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                            item => item.City.Contains(input.Filter) || item.Url.Contains(input.Filter)
                            || item.Browser.Contains(input.Filter) || item.ISP.Contains(input.Filter) 
                            || item.Resolution.Contains(input.Filter))
                            .Where(item => item.VisitTime >= input.StartTime && item.VisitTime < endTime)
                            .Where(item=>item.TenancyName == TenancyName);
            return query;

        }

        public static IQueryable<AccessData> VisitorSystemEnvDetailQuery(this IQueryable<AccessData> query, SystemEnvTaxonomyInput input, string TenancyName)
        {
            var endTime = input.EndTime.AddDays(1);
            query = query.Where(item => item.VisitTime >= input.StartTime && item.VisitTime < endTime)
                        .Where(item=>item.FormFactor == input.FormFactor)
                        .Where(item=>item.TenancyName == TenancyName);
            return query;
        }
        #endregion


        /// <summary>
        /// 详细访问
        /// </summary>
        /// <param name="query"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public static IQueryable<AccessData> AccessDetailRecordQuery(this IQueryable<AccessData> query, AccessDetailRecordInput input)
        {
            var endTime = input.EndTime.AddDays(1);
            query = query.WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                item => item.ClientIp == input.Filter || item.Acount == input.Filter || item.Browser.Contains(input.Filter) || item.Searchkey.Contains(input.Filter))
                .WhereIf(input.Source != null, item => item.SourceType == input.Source.Value)
                .WhereIf(input.Device != null, item => item.FormFactor == input.Device.Value)
                .WhereIf(!input.Search.IsNullOrWhiteSpace(), item => item.EngineType == input.Search)
                .Where(item => item.VisitTime >= input.StartTime && item.VisitTime < endTime);
            return query;
        } 
    }
}
